package com.weipai.springboot.module.controller;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Demand;
import com.weipai.springboot.model.Shop;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.mapper.ShopCartMapper;
import com.weipai.springboot.module.mapper.ShopMapper;
import com.weipai.springboot.module.service.FileUploadService;
import com.weipai.springboot.module.service.PersonalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/27 10:20
 * <p>
 * 个人中心控制类
 */
@RestController
public class PersonalController {

    @Autowired
    PersonalService personalService;
    @Autowired
    FileUploadService fileUploadService;
    @Autowired
    ShopMapper shopMapper;


    /**
     * 获取用户的需求
     *
     * @return
     */
    @GetMapping("/personal/getDemandByCustomerId")
    public Response getDemandByCustomerId() {
        return personalService.getDemandByCustomerId();
    }

    /**
     * 添加需求
     *
     * @param request
     * @param demand
     * @param img
     * @return
     */
    @PostMapping("/personal/saveDemand")
    public Response saveDemand(HttpServletRequest request, Demand demand, MultipartFile img) {
        Response response = new Response();
        if (img != null) {
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                demand.setDemandImg(result);
                response = personalService.saveDemand(demand);
            }
        } else {
            response = personalService.saveDemand(demand);
        }
        return response;
    }

    /**
     * 获取需求分类
     *
     * @return
     */
    @GetMapping("/personal/getDemandCategory")
    public Response getDemandCategory() {
        return personalService.getDemandCategory();
    }

    /**
     * 申请店铺--------------------------------------------------------------APP端
     *
     * @param shop 对象
     *             shopLogoImg             logo图片
     *             businessLicenseImg     营业执照图片
     *             customerId
     * @return
     */
    @PostMapping("/personal/applyForShop")
    public Response applyForShop(HttpServletRequest request,
                                 Shop shop,
                                 MultipartFile shopLogoImg,
                                 MultipartFile businessLicenseImg,
                                 String customerId) {
        Response response = new Response();
        if (shopLogoImg != null || businessLicenseImg != null) {
            String shopLogoImgPath = fileUploadService.saveImgUpload(request, shopLogoImg);
            String businessLicenseImgPath = fileUploadService.saveImgUpload(request, businessLicenseImg);
            if (shopLogoImgPath.equals("formatError") || businessLicenseImgPath.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (shopLogoImgPath.equals("sizeover") || businessLicenseImgPath.equals("formatError")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (shopLogoImgPath.equals("error") || businessLicenseImgPath.equals("formatError")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                shop.setShopLogo(shopLogoImgPath);
                shop.setBusinessLicense(businessLicenseImgPath);
                response = personalService.applyForShop(shop, customerId);
            }
        } else {
            response = personalService.applyForShop(shop, customerId);
        }
        return response;
    }


    /**
     * 申请店铺------------------------------------------------------------web端
     *
     * @param shop 对象
     *             shopLogoImg             logo图片
     *             businessLicenseImg     营业执照图片
     *             customerId
     * @return
     */
    @PostMapping("/personal/applyForShopWeb")
    public Response applyForShopWeb(HttpServletRequest request,
                                    Shop shop,
                                    MultipartFile shopLogoimg,
                                    MultipartFile businessLicenseimg,
                                    MultipartFile idCardFrontimg,
                                    MultipartFile idCardReverseimg,
                                    String customerId) {
        Response response = new Response();
//        if(shopLogoimg!=null ||  businessLicenseimg!=null ){
        if (shopLogoimg != null) {
            String shopLogoImgPath = fileUploadService.saveImgUpload(request, shopLogoimg);
            if (shopLogoImgPath.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (shopLogoImgPath.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (shopLogoImgPath.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                shop.setShopLogo(shopLogoImgPath);
            }
        }
        if (businessLicenseimg != null) {
            String businessLicenseImgPath = fileUploadService.saveImgUpload(request, businessLicenseimg);
            if (businessLicenseImgPath.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (businessLicenseImgPath.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (businessLicenseImgPath.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                shop.setBusinessLicense(businessLicenseImgPath);
            }
        }
        if (idCardFrontimg != null) {
            String idCardFrontimgPath = fileUploadService.saveImgUpload(request, idCardFrontimg);
            if (idCardFrontimgPath.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (idCardFrontimgPath.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (idCardFrontimgPath.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                shop.setIdCardFront(idCardFrontimgPath);
            }
        }
        if (idCardReverseimg != null) {
            String idCardReverseimgPath = fileUploadService.saveImgUpload(request, idCardReverseimg);
            if (idCardReverseimgPath.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (idCardReverseimgPath.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (idCardReverseimgPath.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                shop.setIdCardReverse(idCardReverseimgPath);
            }
        }
        response = personalService.applyForShopWeb(shop, customerId);

//        if (shopLogoimg != null && businessLicenseimg != null && idCardFrontimg != null && idCardReverseimg != null) {
//            String shopLogoImgPath = fileUploadService.saveImgUpload(request, shopLogoimg);                 //店铺logo
//            String businessLicenseImgPath = fileUploadService.saveImgUpload(request, businessLicenseimg);  //营业执照
//            String idCardFrontImgPath = fileUploadService.saveImgUpload(request, idCardFrontimg);  //身份证正面
//            String idCardReverseImgPath = fileUploadService.saveImgUpload(request, idCardReverseimg);  //身份证反面
//            if (shopLogoImgPath.equals("formatError") || businessLicenseImgPath.equals("formatError")) {
//                response.setCode(RetCode.IMG_FORMAT);
//            } else if (shopLogoImgPath.equals("sizeover") || businessLicenseImgPath.equals("formatError")) {
//                response.setCode(RetCode.IMG_SIZE);
//            } else if (shopLogoImgPath.equals("error") || businessLicenseImgPath.equals("formatError")) {
//                response.setCode(RetCode.IMG_ERROR);
//            } else {
//                shop.setShopLogo(shopLogoImgPath);
//                shop.setBusinessLicense(businessLicenseImgPath);
//                shop.setIdCardFront(idCardFrontImgPath);
//                shop.setIdCardReverse(idCardReverseImgPath);
//                response = personalService.applyForShopWeb(shop, customerId);
//            }
//        } else {
//            response = personalService.applyForShopWeb(shop, customerId);
//        }
        return response;
    }


    /**
     * 获取店铺分类
     *
     * @return
     */
    @GetMapping("/personal/getShopCategory")
    public Response getShopCategory() {
        return personalService.getShopCategory();
    }

    /**
     * 用户反馈
     *
     * @param feedText：反馈内容
     * @return
     */
    @PostMapping("/personal/customerFeedBack")
    public Response customerFeedBack(String feedText) {
        return personalService.customerFeedBack(feedText);
    }

    /**
     * 用户的优惠券---------------------优惠券列表
     *
     * @param status：0：未使用，1已使用，-1已失效 giftCertificateStatus  0未开启，1开启，-1失效',
     * @param page
     * @param size                    isUse    0未使用，1表示使用'
     * @return
     */
    @GetMapping("/personal/getCustomerGift/{status}/{page}/{size}/{customerId}")
    public Response getCustomerGift(@PathVariable("status") Integer status,
                                    @PathVariable("page") Integer page,
                                    @PathVariable("customerId") String customerId,
                                    @PathVariable("size") Integer size) {
        return personalService.getCustomerGift(status, page, size, customerId);
    }

    /**
     * 领取优惠券
     *
     * @param giftId
     * @return
     */
    @GetMapping("/personal/receiveGift/{giftId}/{customerId}")
    public Response receiveGift(@PathVariable("giftId") String giftId,
                                @PathVariable("customerId") String customerId) {
        return personalService.receiveGift(giftId, customerId);
    }

    /**
     * 保存评论
     *
     * @param request
     * @param spuNumber：货品编号
     * @param evaluateText：评论内容
     * @param star：星级
     * @param img：上传图片
     * @return
     */
    @PostMapping("/personal/saveEvaluate")
    public Response saveEvaluate(HttpServletRequest request, String goodsSpec, String spuNumber,
                                 String evaluateText, Integer star, MultipartFile[] img, Integer isAnonymity, String goodsId) {
        Response response = new Response();
        if (img != null && img.length > 0) {
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                response = personalService.saveEvaluate(goodsSpec, evaluateText, spuNumber, star, result, isAnonymity, goodsId);
            }
        } else {
            response = personalService.saveEvaluate(goodsSpec, evaluateText, spuNumber, star, "", isAnonymity, goodsId);
        }
        return response;
    }


    /**
     * 上传身份证 正反面
     * <p>
     * frontImg   正面图片
     * backImg     反面图片
     * shopId
     */
    @RequestMapping("/personal/idCardImgUpdate")
    public Response idCardImgUpdate(HttpServletRequest request,
                                    MultipartFile frontImg,
                                    MultipartFile backImg,
                                    String shopId) {
        Response response = new Response();
        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        if (frontImg != null && backImg != null) {
            String frontImgPath = fileUploadService.saveImgUpload(request, frontImg);
            String backImgPath = fileUploadService.saveImgUpload(request, backImg);
            if (frontImgPath.equals("formatError") || backImgPath.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (frontImgPath.equals("sizeover") || backImgPath.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (frontImgPath.equals("error") || backImgPath.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                shop.setIdCardFront(frontImgPath);
                shop.setIdCardReverse(backImgPath);
                shopMapper.updateByPrimaryKeySelective(shop);
                response.setCode(RetCode.SUCCESS);
            }
        } else {
            response.setCode(RetCode.IMG_ERROR);
        }
        return response;
    }

    /*  验证身份证是否有效

     */
    @RequestMapping("/personal/varIdCardImg")
    public Response varIdCardImg(String shopId) {
        return personalService.varIdCardImg(shopId);
    }



    /*  *//**
     * 提交身份证
     * @param img
     * @param type
     * @return
     *//*
    @GetMapping("/personal/identificationCard/{img}/{type}")
    public Response identificationCard(@PathVariable("img") String img, @PathVariable("type") String type){
        return personalService.identificationCard(img, type);
    }
    */

    /**
     * 获取用户的积分记录
     *
     * @return
     */
    @GetMapping("/personal/getCustomerIntegral")
    public Response getCustomerIntegralInfo() {
        return personalService.getCustomerIntegralInfo();
    }


}
